package com.stenio.fancat.controller.baby;

import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.controller.Result;
import com.stenio.fancat.domain.bo.BabyPartnerBO;
import com.stenio.fancat.form.baby.BabyPartnerForm;
import com.stenio.fancat.service.baby.BabyPartnerService;
import com.stenio.fancat.vo.BabyPartnerVO;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/partners")
public class BabyPartnerController {

    @Autowired
    private BabyPartnerService babyPartnerService;

    /**
     * 查看宝宝伙伴
     * @param babyPartnerForm
     * @return
     */
    @PostMapping("/selectPartners")
    public Result<List<BabyPartnerBO>> selectPartners(@RequestBody BabyPartnerForm babyPartnerForm){
        Result<List<BabyPartnerBO>> result = new Result();
        List<BabyPartnerBO> list = null;
        try {
            list = babyPartnerService.selectPartners(babyPartnerForm.getUserId());
        } catch (FanCatException e) {
            result.setMsg(e.getMessage());
            result.setFlag(Result.FLAG.failure);
            return result;
        }

        result.setData(list);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 根据userId查询小伙伴的总数
     * @param babyPartnerForm
     * @return
     */
    @PostMapping("/selectCounts")
    public Result<Integer> selectCounts(@RequestBody BabyPartnerForm babyPartnerForm){
        Result<Integer> result = new Result<>();
        Integer number = null;
        try {
            number = babyPartnerService.selectCounts(babyPartnerForm.getUserId());
        } catch (FanCatException e) {
            result.setMsg(e.getMessage());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(number);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 邀请小伙伴进入伙伴圈
     * @param babyPartnerForm
     * @return
     */
    @PostMapping("/InvitePartner")
    public Result<Integer> InvitePartner(@RequestBody BabyPartnerForm babyPartnerForm){
        Result<Integer> result = new Result<>();
        Integer number = null;
        try {
            number = babyPartnerService.InvitePartner(babyPartnerForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMessage());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(number);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 被邀请小伙伴的页面
     * @param babyPartnerForm
     * @return
     */
    @PostMapping("/invitedPage")
    public Result<BabyPartnerVO> invitedPage(@RequestBody BabyPartnerForm babyPartnerForm){
        Result<BabyPartnerVO> result = new Result<>();
        BabyPartnerVO babyPartnerVO = null;
        try {
            babyPartnerVO = babyPartnerService.invitedPage(babyPartnerForm.getUserId());
        } catch (FanCatException e) {
            result.setMsg(e.getMessage());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(babyPartnerVO);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 领糖果币（candy）  如果领完变成已领取
     * @param babyPartnerForm
     * @return
     */
    @PostMapping("/getCandy")
    public Result<Integer> getCandy(@RequestBody BabyPartnerForm babyPartnerForm){
        Result<Integer> result = new Result<>();
        Integer number = null;
        try {
            number = babyPartnerService.getCandy(babyPartnerForm);
        } catch (FanCatException e) {
            result.setMsg(e.getMessage());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(number);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    /**
     * 查询最近邀请的小伙伴（已经邀请到的小伙伴）  ---   展示出全部
     * @param babyPartnerForm
     * @return
     */
    @PostMapping("/selectPartnersList")
    public Result<List<BabyPartnerBO>> selectPartnersList(@RequestBody BabyPartnerForm babyPartnerForm){
        Result<List<BabyPartnerBO>> result = new Result<>();
        List<BabyPartnerBO> list = null;
        try {
            list = babyPartnerService.selectPartnersList(babyPartnerForm.getUserId());
        } catch (FanCatException e) {
            result.setMsg(e.getMessage());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(list);
        result.setFlag(Result.FLAG.success);
        return result;
    }

    @PostMapping("/judgeRelation")
    public Result<Integer> judgeRelation(@RequestBody BabyPartnerForm babyPartnerForm){
        Result<Integer> result = new Result<>();
        Integer state = null;
        try {
            state = babyPartnerService.judgeRelation(babyPartnerForm.getUserId(),babyPartnerForm.getAssociatedUserId());
        } catch (FanCatException e) {
            result.setMsg(e.getMessage());
            result.setFlag(Result.FLAG.failure);
            return result;
        }
        result.setData(state);
        result.setFlag(Result.FLAG.success);
        return result;
    }
}
